﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
using LabLibary.ExtractData;

namespace LabLibary
{
    public static class LabExtractData
    {

        #region Khai bao bien 
        static string fileNamePort01 = String.Empty;
        static string fileNamePort02 = String.Empty;
        static string fileNamePort03 = String.Empty;
        static string fileNamePort04 = String.Empty;
        static string fileNamePort05 = String.Empty;
        static string fileNamePort06 = String.Empty;
        static string fileNamePort07 = String.Empty;
        static string fileNamePort08 = String.Empty;

        static Dictionary<string, string> dcMapPortName_File = new Dictionary<string, string> {
            {"port01", fileNamePort01},
            {"port02", fileNamePort02},
            {"port03", fileNamePort03},
            {"port04", fileNamePort04},
            {"port05", fileNamePort05},
            {"port06", fileNamePort06},
            {"port07", fileNamePort07},
            {"port08", fileNamePort08}
        };

        #endregion

        #region Dodata
        public static List<ObjResults> DoData(string _machineName, string _data, string _port)
        {
            if (_machineName == "Cobas C311")
                return DoData_C311(_data, _port);

            else if (_machineName == "Cobas E411")
            {
                return DoData_E411(_data, _port);
            }

            else if (_machineName == "Mission U120")
            {
                return DoData_Mission_U120(_data, _port);
            }

            else if (_machineName == "BC3000")
            {
                return DoData_BC3000(_data, _port);
            }

            else if (_machineName == "ElecSys 1020")
            {
                return DoData_ElecSys1020(_data, _port);
            }

            else if (_machineName == "Cybow 300")
            {
                return DoData_Cybow300(_data, _port);
            }

            else if (_machineName == "Hytachi 911")
            {
                return DoData_Hytachi_911(_data, _port);
            }

            else if (_machineName == "Other")
            {
                return DoData_Other(_data, _port);
            }

            return null;
        }
        #endregion



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Others Machine
        /// 
        public static List<ObjResults> DoData_Other(string _data, string _port)
        {

            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (Convert.ToInt32(_data[0]).ToString() == "5")
            {
                fileName = @"data\other_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.Write("Other \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                //sw.Write(data.Remove(data.Length - 1, 1));
                sw.Write(_data);
            }

            //_data = _data.Replace("\r", string.Empty);

            //if (Convert.ToInt32(_data[_data.Length - 1]).ToString() == "4")
            //{
              //  return CobasC311.ExtactData(fileName);
            //}

            //return null;
            return null;
        }


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Cobas C311
        /// 
        #region Extract Result Cobas C311
        public static List<ObjResults> DoData_C311(string _data, string _port)
        {
            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (Convert.ToInt32(_data[0]).ToString() == "5")
            {
                fileName = @"data\cb_c311_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.Write("Cobas C311 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                //sw.Write(data.Remove(data.Length - 1, 1));
                sw.Write(_data);
            }



            //Extract data
            //if (Convert.ToInt32(_data[_data.Length - 2]).ToString() == "4")
            //if (Convert.ToInt32(_data[_data.Length - 1]).ToString() == "4")

            //Remove the "Enter key" of _data
            _data = _data.Replace("\r", string.Empty);

            if (Convert.ToInt32(_data[_data.Length - 1]).ToString() == "4")
            {

                #region Extract

                //string _SID = string.Empty;

                //List<ObjResults> _lstResults = new List<ObjResults>();
                //ObjResults _objResults = new ObjResults();


                //string result = string.Empty;
                //try
                //{
                //    using (StreamReader sr = new StreamReader(fileName))
                //    {
                //        string line;
                //        while ((line = sr.ReadLine()) != null)
                //        {
                //            try
                //            {
                //                if (line[0] == 'O')
                //                {
                //                    string[] _tmpSplit = line.Split('|');
                //                    result += "ID: " + _tmpSplit[2].Trim() + "\n";

                //                    _SID = _tmpSplit[2].Trim();
                //                }

                //                if (line[0] == 'R')
                //                {
                //                    string[] _tmpSplit = line.Split('|');
                //                    result += "LabID: " + _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("/", string.Empty);
                //                    result += " - Value: " + _tmpSplit[3].Trim() + "\n";

                //                    _lstResults.Add(new ObjResults(_SID, _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("/", string.Empty), _tmpSplit[3].Trim()));
                //                }
                //            }
                //            catch { }
                //        }

                //        try
                //        {
                //            sr.Close();
                //            File.Delete(fileName);
                //        }
                //        catch { }
                //    }
                //}
                //catch
                //{
                //    result = null;
                //}
                //return _lstResults;

                #endregion

                return CobasC311.ExtactData(fileName);
            }

            return null;
        }
        #endregion


        /// /////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Cobas E411
        /// 
        #region Extract Result Cobas E411
        public static List<ObjResults> DoData_E411(string _data, string _port)
        {
            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (Convert.ToInt32(_data[0]).ToString() == "5")
            {
                fileName = @"data\cb_e411_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;
                

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.Write("Cobas E411 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                //sw.Write(data.Remove(data.Length - 1, 1));
                sw.Write(_data);
            }


            //Extract data
            //if (Convert.ToInt32(_data[_data.Length - 1]).ToString() == "4")

            if (Convert.ToInt32(_data[0]).ToString() == "4")
            {
                #region Extract

                //string _SID = string.Empty;
                //string _LabMaChineId = string.Empty;
                //string _Value = string.Empty;

                //List<ObjResults> _lstResults = new List<ObjResults>();
                //ObjResults _objResults = new ObjResults();

                //string result = string.Empty;
                //try
                //{
                //    using (StreamReader sr = new StreamReader(fileName))
                //    {
                //        string line;

                //        while ((line = sr.ReadLine()) != null)
                //        {
                //            try
                //            {
                //                line = line.Remove(0, 2);

                //                if (line[0] == 'O')
                //                {
                //                    string[] _tmpSplit = line.Split('|');
                //                    result += "ID: " + _tmpSplit[2].Trim() + "\n";

                //                    _SID = _tmpSplit[2].Trim();
                //                }

                //                if (line[0] == 'R')
                //                {
                //                    string[] _tmpSplit = line.Split('|');
                //                    //result += "LabID: " + _tmpSplit[2].Trim().Replace("^", string.Empty).Replace("^0", string.Empty);
                //                    //result += " - Value: " + _tmpSplit[3].Trim() + "\n";

                //                    _LabMaChineId = _tmpSplit[2].Trim().Replace("^0", string.Empty).Replace("^", string.Empty);
                //                    _Value = _tmpSplit[3].Trim().Replace("-1^", string.Empty);

                //                    _lstResults.Add(new ObjResults(_SID, _LabMaChineId, _Value));
                //                }
                //            }
                //            catch { }
                //        }

                //        try
                //        {
                //            sr.Close();
                //            //File.Delete(fileName);
                //        }
                //        catch { }

                //        return _lstResults;
                //    }
                //}
                //catch
                //{
                //    result = null;
                //}
                //return _lstResults;

                #endregion

                return CobasE411.ExtactData(fileName);
            }

            return null;
        }
        #endregion



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Mission U120
        /// 
        #region Extract Result Mission U120
        public static List<ObjResults> DoData_Mission_U120(string _data, string _port)
        {
            string fileName = "";
            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            //Write data
            char[] tmpchar = _data.ToCharArray();
            if (Convert.ToInt32(tmpchar[0]).ToString() == "2")
            {
                fileName = @"data\ms_u120_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    sw.Write("Mission U120 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                //sw.Write(_data.Remove(_data.Length - 1, 1));
                sw.Write(_data);
            }


            //Read Data
            _data = _data.Replace("\r", string.Empty);
            tmpchar = _data.ToCharArray();
            if (Convert.ToInt32(tmpchar[tmpchar.Length - 1]).ToString() == "3")
            {
                #region Extract data Mission U120
                //try
                //{
                //    List<ObjResults> _lstResults = new List<ObjResults>();
                //    ObjResults _objResults = new ObjResults();

                //    string _SID = string.Empty;
                //    string _LabMaChineId = string.Empty;
                //    string _Value = string.Empty;


                   
                //    string result = string.Empty;
                //    try
                //    {
                //        using (StreamReader sr = new StreamReader(fileName))
                //        {
                //            string line;
                //            while ((line = sr.ReadLine()) != null)
                //            {
                //                if (line.Contains("ID"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "ID:");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "ID: " + _tmpSplit[1] + "\n";

                //                        _SID = _tmpSplit[1];
                //                    }
                //                }

                //                else if (line.Contains("Date"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "Date:");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "Date: " + _tmpSplit[1] + "\n";
                //                    }
                //                }

                //                else if (line.Contains("LEU"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "LEU");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "LEU: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "LEU", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("NIT"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "NIT");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "NIT: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "NIT", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("URO"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "URO");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "URO: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "URO", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("PRO"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "PRO");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "PRO: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "PRO", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("pH"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "pH");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "pH: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "pH", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("BLO"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "BLO");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "BLO: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "BLO", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("SG"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "SG");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "SG: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "SG", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("KET"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "KET");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "KET: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "KET", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("BIL"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "BIL");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "BIL: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "BIL", _tmpSplit[1].Trim()));
                //                    }
                //                }

                //                else if (line.Contains("GLU"))
                //                {
                //                    string[] _tmpSplit = Regex.Split(line, "GLU");
                //                    if (_tmpSplit.Length > 1)
                //                    {
                //                        result += "GLU: " + _tmpSplit[1].Trim() + "\n";

                //                        _lstResults.Add(new ObjResults(_SID, "GLU", _tmpSplit[1].Trim()));
                //                    }
                //                }
                //            }
                //            sr.Close();


                //            return _lstResults;
                //        }
                //    }
                //    catch { return null; }
                //}
                //catch (Exception ex)
                //{
                //    Console.WriteLine("The file could not be read:");
                //    Console.WriteLine(ex.Message);
                //}

                #endregion

                return MissionU120.ExtactData(fileName);
            }

            return null;
        }
        #endregion



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// HH BC3000
        /// 
        #region Extract Result BC3000
        public static List<ObjResults> DoData_BC3000(string _data, string _port)
        {
            string fileName = "";
            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;



            //Write data
            char[] tmpchar = _data.ToCharArray();
            if (Convert.ToInt32(tmpchar[0]).ToString() == "2")
            {
                fileName = @"data\bc3000_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    //sw.Write("BC3000 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                _data = _data.Replace('\n', ' ');
                sw.Write(_data);
            }


            //Read Data
            _data = _data.Replace("\r", string.Empty);
            tmpchar = _data.ToCharArray();
            if (Convert.ToInt32(tmpchar[tmpchar.Length - 1]).ToString() == "26")
            {
                return BC3000.ExtactData(fileName);
            }

            return null;
        }
        #endregion



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// HH ElecSys1020
        /// 
        public static List<ObjResults> DoData_ElecSys1020(string _data, string _port)
        {
            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (Convert.ToInt32(_data[0]).ToString() == "5")
            {
                fileName = @"data\Elecsys1020_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    //sw.Write("ElecSys1020");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                sw.Write(_data);
            }

            _data = _data.Replace("\r", string.Empty);
            if (Convert.ToInt32(_data[_data.Length - 1]).ToString() == "4")
            {
                return ElecSys1020.ExtactData(fileName);
            }

            return null;
        }


        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// Nuoc Tieu Cybow300
        /// 
        private static List<ObjResults> DoData_Cybow300(string _data, string _port)
        {
            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (_data[0].ToString() == "~")
            {
                fileName = @"data\Cybow300_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    //sw.Write("ElecSys1020 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                sw.Write(_data);
            }

            _data = _data.Replace("\r", string.Empty).Replace("\n", string.Empty);
            _data = _data.Trim();

            //LabCommon.ShowMessage(_data);

            if (_data[_data.Length - 1].ToString() == "~")
            {
                return Cybow300.ExtactData(fileName);
            }

            return null;
        }



        /////////////////////////////////////////////////////////////////////////////////////////////////////////////
        /// SH Hytachi 911
        ///
        private static List<ObjResults> DoData_Hytachi_911(string _data, string _port)
        {
            _data = _data.Replace("\n", string.Empty).Trim();

            string fileName = "";

            if (_port == "port01")
                fileName = fileNamePort01;
            else if (_port == "port02")
                fileName = fileNamePort02;
            else if (_port == "port03")
                fileName = fileNamePort03;
            else if (_port == "port04")
                fileName = fileNamePort04;
            else if (_port == "port05")
                fileName = fileNamePort05;
            else if (_port == "port06")
                fileName = fileNamePort06;
            else if (_port == "port07")
                fileName = fileNamePort07;
            else if (_port == "port08")
                fileName = fileNamePort08;


            if (Convert.ToInt32(_data[0]).ToString() == "2")
            {
                fileName = @"data\Hytachi911_" + DateTime.Now.ToFileTime().ToString() + ".rs";

                if (_port == "port01")
                    fileNamePort01 = fileName;
                else if (_port == "port02")
                    fileNamePort02 = fileName;
                else if (_port == "port03")
                    fileNamePort03 = fileName;
                else if (_port == "port04")
                    fileNamePort04 = fileName;
                else if (_port == "port05")
                    fileNamePort05 = fileName;
                else if (_port == "port06")
                    fileNamePort06 = fileName;
                else if (_port == "port07")
                    fileNamePort07 = fileName;
                else if (_port == "port08")
                    fileNamePort08 = fileName;

                using (StreamWriter sw = File.CreateText(fileName))
                {
                    //sw.Write("ElecSys1020 \n");
                }
            }

            using (StreamWriter sw = File.AppendText(fileName))
            {
                sw.Write(_data);
            }

            string _tmpdata = _data = _data.Trim();
            _tmpdata = _tmpdata.Remove(_tmpdata.Length-2,2);

            if (Convert.ToInt32(_tmpdata[_tmpdata.Length -1]).ToString() == "3")
            {
                return Hytachi_911.ExtactData(fileName);
            }

            return null;
        }
    }
}
